package by.semerikov.dao.factory.mysqlJdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import by.semerikov.dao.Person;
import by.semerikov.dao.Proposal;
import by.semerikov.dao.UserSession;
import by.semerikov.data.Connector;
import by.semerikov.exceptions.BadObjectException;
import by.semerikov.exceptions.DAOException;

public class MySQLDAOService { 
	public MySQLDAOService() {
		
		
	}
	
	public List<Person> getPersonsEqual(Person example) throws DAOException
	{
		List<Person> result = new ArrayList<Person>();
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			PersonDAO pd = new PersonDAO(conn);
			result = pd.read(example);
			conn.setAutoCommit(true);
			conn.close();
		} catch (SQLException e) {
			throw new DAOException(e);
		}
		return result;
		
	}

	public void createUserSession(UserSession object) throws DAOException
	{
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			UserSessionDAO usd = new UserSessionDAO(conn);
			usd.create(object);
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
	
	public List<UserSession> readUserSession(UserSession object) throws DAOException
	{
		List<UserSession> result = new ArrayList<UserSession>();
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			UserSessionDAO usd = new UserSessionDAO(conn);
			result = usd.read(object);
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
		return result;
	}
	
	public void deleteUserSession(UserSession object) throws DAOException
	{
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			UserSessionDAO usd = new UserSessionDAO(conn);
			usd.delete(object);
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
	
	public void createProposal(Proposal object) throws DAOException
	{
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			ProposalDAO prd = new ProposalDAO(conn);
			prd.create(object);
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
	
	public List<Proposal> getProposal(Proposal example) throws DAOException
	{
		List<Proposal> result = new ArrayList<Proposal>();
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			ProposalDAO pd = new ProposalDAO(conn);
			result = pd.read(example);
			conn.setAutoCommit(true);
			conn.close();
		} catch (SQLException e) {
			throw new DAOException(e);
		}
		catch (BadObjectException e) {
			throw new DAOException(e);
		}
		return result;
		
	}
	
	public void deleteProposal(Proposal object) throws DAOException
	{
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			ProposalDAO prd = new ProposalDAO(conn);
			prd.delete(object);
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
	
	public void updateProposal(Proposal object) throws DAOException
	{
		try {
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			ProposalDAO prd = new ProposalDAO(conn);
			prd.update(object); 
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
	
}
